package com.masterclient.app.modules.purchase.contract;

import android.widget.Toast;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.bean.aftersale.ShipmentsBean;
import com.eoner.baselibrary.bean.purchase.DeliveryDetailBean;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;


public class PurchaseShippingPresenter extends PurchaseShippingContract.Presenter{

    @Override
    public void getShippingList() {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().getShipmentItems(), new Consumer<ShipmentsBean>() {
            @Override
            public void accept(ShipmentsBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showShippingList(bean.getData().getSh_shipment_companys());
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new ThrowableConsumer()));
    }



    @Override
    public void confirmShipping(int type, String order_id, String track_no,String carrier_code) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id",order_id);
            jsonObject.put("track_no",track_no);
            jsonObject.put("carrier_code",carrier_code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(type == 1?RetrofitUtil2.getInstance().dealerAddShipping(body):RetrofitUtil2.getInstance().dealerShipping(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showResult(true,(type==1?"添加包裹":"发货")+"已提交");
                }else {
                    mView.showResult(false,bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                mView.showResult(false,"网络异常，请重试");
            }
        }));
    }

    @Override
    public void getShippingData(String order_id, String order_express_id) {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().deliveryDetail(order_id, order_express_id), new Consumer<CommonBaseResponse<DeliveryDetailBean>>() {
            @Override
            public void accept(CommonBaseResponse<DeliveryDetailBean> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showData(bean.getData());
                }else {
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                mView.showResult(false,"网络异常，请重试");
            }
        }));
    }
}